Module('Phosphor.Events', function () {
    var Promise = this.Promise;
    
    Promise.Image = function (src, timeout) {
        timeout = timeout || 3000;

        return Promise.from(function (succeed, fail) {
            var image;
            if (typeof Image != 'undefined') {
                image = new Image();
            } else {
                image = document.createElement('img');
            }

            image.src = src;

            window.setTimeout(function () {
                if (image.width && image.height) {
                    succeed(image);
                } else {
                    image.onload = function () {
                        succeed(image);
                    };
                }
            }, 0);

            window.setTimeout(fail, timeout);
        });
    };

    Promise.Images = function (urls, timeout) {
        var promises = urls.map(function (url) {
            return new Promise.Image(url, timeout);
        });

        return Promise.Array(promises);
    };
});
